//
//  MessageVC.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/7/10.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "MessageVC.h"
#import "MessageCell.h"
#import "MessageModel.h"
#import "MessageDetailVC.h"
#import "MessageDM.h"
#import "BdTV.h"
#import "config.h"
#import "AppDelegate.h"

@interface MessageVC ()<UITableViewDataSource,UITableViewDelegate,MessageDelegate>

@property (nonatomic,strong)NSArray *sysMsgArray,*actMsgArray,*orderMsgArray;

@end

@implementation MessageVC




-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [AppDelegate sharedAppDelegate].delegate = self;
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [AppDelegate sharedAppDelegate].delegate = nil;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self initView];
    
    [self refreshData];
    
}



-(void)initView{
    [super initView];
    
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    [title setMianTitle:@"消息"];
    [[title backBtn]setTag:11];
    [[title backBtn]addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    title.doneBtn.hidden = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    
    
}



-(void)refreshData{
    
    __weak typeof(self) baseSelf = self;
    [self.nodataCell setState:nodataCellStateLoading];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        baseSelf.sysMsgArray = [[NSArray alloc]initWithArray:[MessageDM getMessage:MessageTypeSYS]];
        baseSelf.actMsgArray = [[NSArray alloc]initWithArray:[MessageDM getMessage:MessageTypeACTIVITY]];
        baseSelf.orderMsgArray = [[NSArray alloc]initWithArray:[MessageDM getMessage:MessageTypeORDER]];
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [baseSelf.tableView reloadData];
        });
    });
}




-(void)btnOnClick:(UIButton *)sender{
    
    if (sender.tag==11) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.sysMsgArray==nil) {
        return 1;
    }else{
        return 3;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.sysMsgArray==nil) {
        return self.nodataCell;
    }
    
    
    MessageCell *cell = [MessageCell cellWithTableView:tableView];
    if (indexPath.row==0) {
        
        MessageModel *mod;
        if(self.sysMsgArray.count>0){
            
            mod = [self.sysMsgArray firstObject];
        }else{
            mod = [MessageModel new];
            [mod setMessageTitle:@"系统消息"];
            [mod setMessageSub:@"暂无消息"];
            [mod setMessageType:MessageTypeSYS];
            [mod setMessageTime:@""];
        }
        cell.model = mod;
        
    }else if (indexPath.row==1){
        
        MessageModel *mod;
        if(self.actMsgArray.count>0){
            
            mod = [self.actMsgArray firstObject];
        }else{
            mod = [MessageModel new];
            [mod setMessageTitle:@"楼盘活动"];
            [mod setMessageSub:@"暂无活动"];
            [mod setMessageType:MessageTypeACTIVITY];
            [mod setMessageTime:@""];
        }
        cell.model = mod;
    }else{
        
        MessageModel *mod;
        if(self.orderMsgArray.count>0){
            
            mod = [self.orderMsgArray firstObject];
        }else{
            mod = [MessageModel new];
            [mod setMessageTitle:@"交易通知"];
            [mod setMessageSub:@"暂无通知"];
            [mod setMessageType:MessageTypeORDER];
            [mod setMessageTime:@""];
        }
        cell.model = mod;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.sysMsgArray==nil) {
        return tableView.frame.size.height;
    }
    
    return [MessageCell getHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row==0) {
        
        if (self.sysMsgArray.count>0) {
            MessageDetailVC *vc = [MessageDetailVC new];
            vc.type = MessageTypeSYS;
            vc.titleStr = @"系统消息";
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
        
    }else if (indexPath.row==1){
        
        if (self.actMsgArray.count>0) {
            MessageDetailVC *vc = [MessageDetailVC new];
            vc.type = MessageTypeACTIVITY;
            vc.titleStr = @"楼盘活动";
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
        
    }else if (indexPath.row==2){
        
        if (self.orderMsgArray.count>0) {
            MessageDetailVC *vc = [MessageDetailVC new];
            vc.type = MessageTypeORDER;
            vc.titleStr = @"交易通知";
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
    }
}



#pragma mark message receive delegate
- (void) messageReceived:(MessageModel *)msg{
    
    [self refreshData];
}

@end
